Repository navigation
Create separate grammars - #1880
BillWagner wants to merge 1 commit into
Conversation
This PR introduces two separate grammar productions for `record` types in C# 9.
|
|
||
| ```ANTLR | ||
| record_class_base | ||
| : ':' class_type base_argument_list? |
There was a problem hiding this comment.
Note that this uses class_type as the grammar for the base, with a semantic restriction that the base is a record. If we made it a grammar rule that the base was a record, the following would be prohibited:
public record R(int Number, string Message) : object;There was a problem hiding this comment.
object is the base class of all types (§8.2.3); sometimes explicitly, a base for a class is a class_type (§8.2.1) which includes object; and sometimes simplicity, as for value types (§8.3.2).
We can follow this grammar approach by defining object to effectively be a class and a record, something like:
record_base
: ':' record_type base_argument_list?
;
record_type
: type_name // in the same way classes & value types are defined this is where
// the semantic requirement is applied – type_name must be a record
| 'object'
;Or we could keep object as a class with something like:
record_base
: ':' record_base_type base_argument_list?
;
record_base_type
: type_name // a record type
| 'object'
;Yes, they are the same grammar with a non-terminal renamed to remove the implication that object is a class type.
I would recommend this approach as it both meshes with the current approach and avoid using a class production in record_base.
| ### §rec-class-general General | ||
|
|
||
| A record class is a specialized reference type that is optimized for storing data rather than behavior. It provides built-in functionality that would normally require significant “boilerplate” code in a non-record class, such as value-based equality and easy immutability. | ||
| A ***record class*** is a class declared using `record`. The rules for classes apply, subject to the provisions of this subclause. Record classes provide built-in functionality for storing data, such as value-based equality and support for immutability. |
There was a problem hiding this comment.
One comment, which you already know:
- A record is a reference type that is optimised for starting data rather than behaviour – of something like that, i.e. follow the original
- In future: A value record is a value type that is…
record_declatation- In future:
value_record_declaration
Etc. Enough on this here.
To me “A record class is a specialised reference type“ just reads wrong, if we are going to take the “record is a variant of class” then would it not be “A record class is a specialised class type”?
Nigel-Ecma
left a comment
There was a problem hiding this comment.
Just a quick review following @BillWagner comment, a small change suggestion to better mesh with the current grammar approach.
(I added a non-review comment before I decided to review, it is a caller for the standard suggested change – either a record is a variant of class or it is a reference type (sibling?))
|
|
||
| ```ANTLR | ||
| record_class_base | ||
| : ':' class_type base_argument_list? |
There was a problem hiding this comment.
object is the base class of all types (§8.2.3); sometimes explicitly, a base for a class is a class_type (§8.2.1) which includes object; and sometimes simplicity, as for value types (§8.3.2).
We can follow this grammar approach by defining object to effectively be a class and a record, something like:
record_base
: ':' record_type base_argument_list?
;
record_type
: type_name // in the same way classes & value types are defined this is where
// the semantic requirement is applied – type_name must be a record
| 'object'
;Or we could keep object as a class with something like:
record_base
: ':' record_base_type base_argument_list?
;
record_base_type
: type_name // a record type
| 'object'
;Yes, they are the same grammar with a non-terminal renamed to remove the implication that object is a class type.
I would recommend this approach as it both meshes with the current approach and avoid using a class production in record_base.
This PR introduces two separate grammar productions for
recordtypes in C# 9.It's one of the two alternatives discussed in our November meeting.